#pa， id
#list1 => true list1_count => 30
#list2 => true list2_count => 120
namespace :bld do
  desc "Export to image from database"
  task :export_to_img => :environment do
    require 'lib/tasks/bili_ranking_image.rb'
    pa = "#{BILI_PRO_PATH}#{ENV['pa']}\\"
    list1 = ENV['list1'] || true
    list1 = false if list1 == 'false'
    list2 = ENV['list2'] || true
    list2 = false if list2 == 'false'
    list1_count = (ENV['list1_count'] || 30).to_i             #list1_count
    list2_count = (ENV['list2_count'] || list1_count + 90).to_i           #list2_count

    img_maker = BiliRankingImage.new pa
      #=========================================================
      date_now =  Time.now
      weekly =  ENV['id'] ? Weekly.find_by_index(ENV['id']) : Weekly.last
      weekly.update_attribute :rank_from, list1_count
      data = WeeklyResult.find_all_by_weekly_id weekly.id, :order => "score_rank, clicks_rank"
      data[0...list1_count].each{ |d| img_maker.make_ranking_img1 d } if list1
      img_maker.make_ranking_img2 data[(list1 ? list1_count : 0)...list2_count] if list2
      puts "#{Time.now - date_now} second complete"
  end
  desc "Export single"
  task :export_single_to_img => :environment do
    require 'lib/tasks/bili_ranking_image.rb'
    pa = "#{BILI_PRO_PATH}#{ENV['pa']}\\"
    img_maker = BiliRankingImage.new pa
    weekly =  ENV['weekly_index'] ? Weekly.find_by_index(ENV['weekly_index']) : Weekly.last 
    weekly_result = WeeklyResult.find :first, :conditions => ["weekly_id = ? and score_rank = ?", weekly.id, ENV['rank']]
    img_maker.make_ranking_img1 weekly_result
  end
end
